﻿@model System.Data.DataTable
@using Jb.Common.Extensions;
@{
    //ViewBag.LinkField = 
    //ViewBag.ExcludeFields =
    //ViewBag.LinkText =
    //ViewBag.LinkFormat =

    List<String> _ExcludedColumns = new List<String>();
    String _LinkField = "ID";    
    String _LinkText = ViewBag.LinkText as String;
    String _LinkFormat = ViewBag.LinkFormat as String;
    String _AnotherLinkText = ViewBag.AnotherLinkText as String;
    String _AnotherLinkFormat = ViewBag.AnotherLinkFormat as String;
    String _AnotherLinkFieldName = ViewBag.AnotherLinkFieldName as String;
    bool _ShowEditLink = true;
    String _NotFoundMessage = "Sorry! No record found.";
    
    if(ViewBag.ExcludedFields != null){
        _ExcludedColumns = ViewBag.ExcludedFields as List<String>;
    }
        
    if (ViewBag.LinkField != null)
    {
        _LinkField = (String)ViewBag.LinkField;
    }    
    if(ViewBag.ShowEditLink != null){
        _ShowEditLink = (bool)ViewBag.ShowEditLink;
    }
    if (_ShowEditLink && ViewBag.LinkFormat == null)
    {
        throw new ArgumentException(String.Format("Partial View _DataTableList requires LinkFormat to be Set."));
    }
    if (_LinkText.IsNullOrEmpty()){
        _LinkText = "Edit";
    }
    if (ViewBag.NotFoundMessage != null)
    {
        _NotFoundMessage = (String)ViewBag.NotFoundMessage;
    }  
    
    List<String> _ColumnNames = new List<String>();
    
    foreach(System.Data.DataColumn column in Model.Columns)
    {
        _ColumnNames.Add(column.ColumnName);
    }     
}
@functions{
    private bool IsExcludedColumn(String columnName, List<String> columnNames)
    {
        if (columnNames.FirstOrDefault(P => String.Compare(columnName, P, true) == 0) != null)
            return true;

        return false;
    }
}
@if (Model.Rows.Count > 0)
{
    <table class="data-table-list" cellpadding="0" cellspacing="0">
        <tr>
        @foreach (System.Data.DataColumn column in Model.Columns)
        {
            if (!IsExcludedColumn(column.ColumnName, _ExcludedColumns))
            {
                <th>@column.ColumnName</th>
            }
        }
        @if (_ShowEditLink)
        {
            <th>Action</th>
        }
        </tr>
        @foreach (System.Data.DataRow row in Model.Rows)
        {
            <tr>
            @foreach (System.Data.DataColumn column in Model.Columns)
            {
                if (!IsExcludedColumn(column.ColumnName, _ExcludedColumns))
                {
                    <td>@WebUtil.GetFormatedData(row[column.ColumnName])</td>
                }                
            }
            @if (_ShowEditLink)
            {
                <td>
                    <a href="@String.Format(_LinkFormat, row[_LinkField])">@_LinkText</a> - 
                    @if (!_AnotherLinkFormat.IsNullOrEmpty())
                    {
                        if (row["UserName"].ToString().Trim() == "")
                        {
                            <a href="@String.Format(_AnotherLinkFormat, row[_AnotherLinkFieldName], row["UserName"])">@_AnotherLinkText</a>
                        }
                        else
                        {
                            <a href="@String.Format(_AnotherLinkFormat, row[_AnotherLinkFieldName], row["UserName"])">EditUser</a>
                        }

                    }
                </td>   
            }
            </tr>
        }
    </table>
}
else
{
    @Html.InfoMessageBox(_NotFoundMessage)
}